//
//  HTFeedback.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/1.
//

import SwiftUI
import PKHUD
import ProgressHUD

struct HTFeedbackScreen: View {

  @EnvironmentObject private var var_store: HTAppStore
  @Environment(\.presentationMode) var presentationMode
  @State private var var_content: String = ""
  @State private var var_mail: String = ""

  var body: some View {
    HTNavigationBar{
      VStack(alignment: .leading, spacing: 0){
        ScrollView(showsIndicators: false){
          Spacer().frame(height: 10)
          VStack(alignment: .leading, spacing: 0){
            Text("Describe The Problem")
              .font(.custom("Helvetica-Bold", size: 14))
              .fontWeight(.heavy)
              .ht_normal()
            Spacer().frame(height: 3)
            ZStack(alignment: .topLeading){
              HTTextEditor(var_text: $var_content, var_backgroundColor: Color.white)
                .font(.system(size: 12))
                .foregroundColor(Color(hex: "#393939"))
                .autocapitalization(.words)
                .disableAutocorrection(true)
                .multilineTextAlignment(.leading)
                .frame(maxWidth: .infinity, minHeight: 150, maxHeight: 150, alignment: .topLeading)
                .padding(.leading, -5)

              if (var_content.isEmpty) {
                Text("Please describe your issue or suggestion in detail.")
                  .font(.system(size: 12))
                  .foregroundColor(Color(UIColor.placeholderText))
                  .padding(.top, 10)
              }
            }
          }
          .padding(15)
          .background(Color.white)
          .frame(maxWidth: .infinity)

          Spacer().frame(height: 10)
          VStack(alignment: .leading, spacing: 0){
            Text("Email Address")
              .font(.custom("Helvetica-Bold", size: 14))
              .fontWeight(.heavy)
              .ht_normal()
            Spacer().frame(height: 3)
            TextField("Please enter a proper valid email address.", text: $var_mail)
              .font(.system(size: 12))
              .foregroundColor(Color(hex: "#393939"))
              .multilineTextAlignment(.leading)
              .frame(maxWidth: .infinity, minHeight: 40, maxHeight: .infinity, alignment: .topLeading)
          }
          .padding(15)
          .background(Color.white)
          .frame(maxWidth: .infinity)

          Spacer().frame(height: 50)
          Button {
            ht_submit()
          } label: {
            Text("Submit")
              .font(.system(size: 14))
              .fontWeight(.bold)
              .foregroundColor((var_content != "" && var_mail != "") ? Color.white : Color(hex: "#999999"))
              .frame(maxWidth: .infinity, maxHeight: .infinity)
          }
          .frame(maxWidth: .infinity, minHeight: 36, maxHeight: 36)
          .background((var_content != "" && var_mail != "") ? Color(hex: "#FF6C3C") : Color(hex: "#000000").opacity(0.04))
          .clipShape(Capsule())
          .padding(.horizontal, 80)

          Spacer().frame(height: 20)
          Text("Or you can contact us at: wizmock@outlook.com")
            .font(.system(size: 12))
            .foregroundColor(Color(hex: "#999999"))
        }
        .frame(maxWidth: .infinity)
      }
      .background(Color(hex: "#f5f5f5"))
      .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
    }
    .ht_title(title: "Feedback")
    .onAppear{
      UITextView.appearance().backgroundColor = .clear
    }
    .onDisappear() {
      UITextView.appearance().backgroundColor = nil
    }
  }

  func ht_submit() {
    if (var_content == "") {
        ProgressHUD.failed("Please describe your issue or suggestion in detail.", delay: 1.0)
      return
    }

    if (var_mail == "") {
      ProgressHUD.failed("Please enter a proper valid email address.", delay: 1.0)
      return
    }

    var_store.dispatch(HTEnumAnimeAction.ht_feedback(content: var_content, andMail: var_mail, andCallback: {
      presentationMode.wrappedValue.dismiss()
    }))
  }
}
